package com.kry.mbs.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jfinal.core.Controller;
import com.kry.mbs.common.ErrorType;
import com.kry.mbs.common.Response;
import com.kry.mbs.model.Bookingrecord;
import com.kry.mbs.model.Meetingroom;
import com.mysql.jdbc.StringUtils;

public class BookingRecordController extends Controller {
	private static final Logger LOGGER = Logger.getLogger("BookingRecordController");

	
	public void list() throws IOException {

		StringBuilder sql = new StringBuilder(" SELECT * FROM bookingrecord ");

		Map<String, String[]> paraMap = getParaMap();
		Set<String> keySet = paraMap.keySet();
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			String value = paraMap.get(key)[0];
			System.out.println("key=" + key + ",value=" + value);
			where.append(" AND ").append(key).append("='").append(value).append("' ");
		}
		System.out.println("sql full:=" + sql.append(where).toString());

		List<Bookingrecord> list = Bookingrecord.dao.find(sql.toString());
		Response<List<Bookingrecord>> response = new Response<List<Bookingrecord>>(ErrorType.OK);
		response.setResult(list);
		renderJson(response);

		// setAttr("bookRecordList", list);
		// renderFreeMarker("/record/list.html");
	}
	
	public void query() throws IOException {
		
		StringBuilder sql = new StringBuilder(" SELECT * FROM bookingrecord ");
		
		Map<String, String[]> paraMap = getParaMap();
		Set<String> keySet = paraMap.keySet();
		StringBuilder where = new StringBuilder(" WHERE 1=1 ");
		StringBuilder orderBy=new StringBuilder(" ORDER BY bookDate, roomNo,startTime ");
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			String value = paraMap.get(key)[0];
			
			//System.out.println("key=" + key + ",value=" + value);
			
			if (StringUtils.isEmptyOrWhitespaceOnly(value)) {
				continue;
			}
			if ("roomNo".equals(key)&&"-1".equals(value)) {//roomNo=-1，指会议室不限
				continue;
			}
			if ("cityCode".equals(key)) {
				where.append(" AND ").append("roomNo").append(" like '").append("10").append(value).append("%' ");
			    continue;
			}

			where.append(" AND ").append(key).append("='").append(value).append("' ");
			
		}
		sql.append(where).append(orderBy);
		System.out.println("sql full:=" + sql.toString());
		
		List<Bookingrecord> list = Bookingrecord.dao.find(sql.toString());
		Response<List<Bookingrecord>> response = new Response<List<Bookingrecord>>(ErrorType.OK);
		response.setResult(list);
		renderJson(response);
		
		// setAttr("bookRecordList", list);
		// renderFreeMarker("/record/list.html");
	}

	public void save() {
		// Bookingrecord bookingrecord=
		// getModel(Bookingrecord.class,"bookingrecord");
		// Bookingrecord bookingrecord= getModel(Bookingrecord.class,"");
		Map<String, String[]> paraMap = getParaMap();
		Set<String> keySet = paraMap.keySet();
		for (Iterator<String> iterator = keySet.iterator(); iterator.hasNext();) {
			String key = (String) iterator.next();
			String value = paraMap.get(key)[0];
			System.out.println("key=" + key + ",value=" + value);
		}
		
		Bookingrecord bookingrecord = getModel(Bookingrecord.class);
		
		
		String roomNo=getPara("roomNo");
		System.out.println("roomNo ::"+roomNo);
		Meetingroom meetingroom=Meetingroom.dao.findFirst("SELECT * FROM meetingroom WHERE roomNo=? ", roomNo);
		
		bookingrecord.setRoomNo(getPara("roomNo"));
		bookingrecord.setRoomName(meetingroom.getRoomName());
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date bookDate=null;
		try {
			bookDate = sdf.parse(getPara("bookDate"));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		
		bookingrecord.setBookDate( bookDate);
		bookingrecord.setStartTime( getPara("startTime"));
		bookingrecord.setEndTime( getPara("endTime"));
		bookingrecord.setBooker(getPara("booker"));
		bookingrecord.setTheme(getPara("theme"));
         
		bookingrecord.save();
		LOGGER.log(Level.INFO, " new bookingrecord's id :" + bookingrecord.get("id"));

		Response<Void> response = new Response<Void>(ErrorType.OK);
		renderJson(response);
	}

	public void edit() {
		setAttr("bookingrecord", Bookingrecord.dao.findById(getParaToInt("id")));
	}

	public void update() {
		getModel(Bookingrecord.class).update();
	}

	public void delete() {
		Bookingrecord.dao.deleteById(getParaToInt("id"));
	}
}
